package com.ruoyi.web.controller.system;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EmergencyType;
import com.ruoyi.system.service.IEmergencyTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 紧急事件类型Controller
 *
 * @author ruoyi
 * @date 2021-06-30
 */
@RestController
@RequestMapping("/system/type")
public class EmergencyTypeController extends BaseController {
    @Autowired
    private IEmergencyTypeService emergencyTypeService;

    /**
     * 查询紧急事件类型列表
     */
//    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmergencyType emergencyType) {
        startPage();
        List<EmergencyType> list = emergencyTypeService.selectEmergencyTypeList(emergencyType);
        return getDataTable(list);
    }

    /**
     * 导出紧急事件类型列表
     */
//    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "紧急事件类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EmergencyType emergencyType) {
        List<EmergencyType> list = emergencyTypeService.selectEmergencyTypeList(emergencyType);
        ExcelUtil<EmergencyType> util = new ExcelUtil<EmergencyType>(EmergencyType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 获取紧急事件类型详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{etId}")
    public AjaxResult getInfo(@PathVariable("etId") Long etId) {
        return AjaxResult.success(emergencyTypeService.selectEmergencyTypeById(etId));
    }
    /**
     * 新增紧急事件类型
     */
//    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "紧急事件类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmergencyType emergencyType) {
        if(null==emergencyType.getEtParent()){
            emergencyType.setEtParent(-1L);
        }
        emergencyTypeService.insertEmergencyType(emergencyType);
        return AjaxResult.success(emergencyTypeService.selectEmergencyTypeById(emergencyType.getEtId()));
    }

    /**
     * 修改紧急事件类型
     */
//    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "紧急事件类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmergencyType emergencyType) {
        return toAjax(emergencyTypeService.updateEmergencyType(emergencyType));
    }

    /**
     * 删除紧急事件类型
     */
//    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "紧急事件类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{etIds}")
    public AjaxResult remove(@PathVariable Long[] etIds) {
        return toAjax(emergencyTypeService.deleteEmergencyTypeByIds(etIds));
    }
}
